#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include "Network.h"
#include "parser.h"


using namespace std;

//Constructor
Network::Network()
{
	T = 0;
}

//Destructor
Network::~Network()
{
	
}

//Opens the input file with the encoding of the network and puts it in the Network class.
//Returns 1 if successful and 0 if unsuccessful
int Network::read_network(string filename)
{	
	ifstream input(filename.c_str());
	Parser p(input);
	int element;
	unsigned int current_line_number;
	unsigned int previous_line_number;
	current_line_number = previous_line_number = 1;
	vector<int> v;
	while((element = p.GetElement())!= -1)
	{	
		current_line_number = p.LineNo();
		/*cout << "Prev line no " << previous_line_number << endl;
		cout << "Curr line no " << current_line_number << endl;
		cout << "Element " << element << endl;*/
		if(current_line_number != previous_line_number)
		{
			/*cout << "Pushing into network\n";
			cout << "The vector is:\n";
			for(vector<int>::iterator it = v.begin() ; it < v.end() ; it++)
			{
				cout << *it << " ";
			}
			cout << "\n";*/
			previous_line_number = current_line_number;
			net.push_back(v);
			v.clear();
			if(element!=-2)
				v.push_back(element);
		}
		else
		{
			//cout << "Pushing into vector\n";
			v.push_back(element);
		}
	}
		
		
	net.push_back(v);
	
	T = p.LineNo();
	cout << "T = "<<T<<"\n";
	
	return 1;
}

void Network::print()
{
	vector<vector<int> >::iterator time;
	for(time = net.begin() ; time < net.end() ; time++)
	{
		vector<int> graph = *time;
		vector<int>::iterator it;
		for(it = graph.begin() ; it < graph.end() ; it++)
		{
			cout << *it << " ";
		}
		cout<<"\n";
	} 
}

vector<int> Network::get_timestep()
{
	vector<int> graph;
	graph = net.front();
	net.erase(net.begin());
	return graph;
}

int Network::get_epoch()
{
	return (T);
}

